package com.lxw.oa.service.serviceImpl;

import com.lxw.oa.entity.SystemLog;
import com.lxw.oa.entity.User;
import com.lxw.oa.mapper.BaseMapper;
import com.lxw.oa.mapper.RoleMapper;
import com.lxw.oa.mapper.UserMapper;
import com.lxw.oa.service.BaseService;
import com.lxw.oa.util.BootstrapTable;
import com.lxw.oa.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("baseService")
@Transactional
public class BaseServiceImpl implements BaseService {


    @Autowired
    private BaseMapper baseMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Map<String,Object>> getPage(BootstrapTable bootstrapTable) {
        Map<String,Object> map=new HashMap<String,Object>();
        map=bootstrapTable.getParams();
        String sqlStr=StringUtil.toSqlSearchStr(map);
        int pageS=bootstrapTable.getPageSize()*(bootstrapTable.getPageNo()-1);
        map.put("pageS",pageS);
        map.put("pageE",bootstrapTable.getPageSize());
        map.put("sqlStr",sqlStr);

        map.put("sort",bootstrapTable.getSort());

        map.put("order",bootstrapTable.getOrder());

        return  baseMapper.getPage(map);
    }

    @Override
    public List<Map<String, Object>> getExportMessage(BootstrapTable bootstrapTable) {
        Map<String,Object> map=new HashMap<String,Object>();
        map=bootstrapTable.getParams();
        String sqlStr=StringUtil.toSqlSearchStr(map);
        map.put("sqlStr",sqlStr);

        map.put("sort",bootstrapTable.getSort());

        map.put("order",bootstrapTable.getOrder());

        return  baseMapper.getExportMessage(map);
    }



    @Override
    public int getCnt(BootstrapTable bootstrapTable) {
        Map<String,Object> map=new HashMap<String,Object>();
        map=bootstrapTable.getParams();
        String sqlStr=StringUtil.toSqlSearchStr(map);
        map.put("sqlStr",sqlStr);
        return baseMapper.getCnt(map);
    }

    @Override
    public List<Map<String,String>> getFields(String tableName) {
        return baseMapper.getFields(tableName);
    }
    @Override
    public  void add(String messageAll){}

    @Override
    public void add(String[] column, String[] value,String tableName) {
        String columnStr=StringUtil.toSqlStr(column,value)[0];
        String valueStr=StringUtil.toSqlStr(column,value)[1];
        Map<String,String>map=new HashMap<String,String>();
        map.put("column",columnStr);
        map.put("value",valueStr);
        map.put("tableName",tableName);
        baseMapper.add(map);
    }

    @Override
    public void edt(String id, String[] column, String[] value, String tableName) {
        String sqlStr=StringUtil.toSqlUpdateStr(column,value);
        Map<String,String>map=new HashMap<String,String>();
        map.put("sqlStr",sqlStr);
        map.put("id",id);
        map.put("tableName",tableName);
        baseMapper.edt(map);
    }

    @Override
    public void del(String id, String tableName) {
        Map<String,String>map=new HashMap<String,String>();
        map.put("id",id);
        map.put("tableName",tableName);
        baseMapper.del(map);
    }

    @Override
    public void setRoleMenu(String roleId, String menuIds) {
        String[]menus=menuIds.split(",");
        String ids="";
        for(int i=0;i<menus.length;i++){
            ids+="'"+menus[i]+"',";
        }
        ids=ids.substring(0,ids.length()-1);
        ids="("+ids+")";
        List<String>list=baseMapper.role_TopMenu_get(ids);
        for(String demo:menus){
            list.add(demo);
        }

        String values="";
        for(int i=0;i<list.size();i++){
            values+=" ('"+roleId+"','"+list.get(i)+"'),";
        }

        values=values.substring(0,values.length()-1);
        if(menus.length==0){
            baseMapper.role_menu_del(roleId);
        }else{
            baseMapper.role_menu_del(roleId);
            baseMapper.role_menu_add(values);
        }

    }

    @Override
    public void setUserRole(String userId, String roleIds) {
        String[]roles=roleIds.split(",");
        String values="";
        String roleStr=" (";
        User user=new User();
        user.setId(Integer.parseInt(userId));
        for(int i=0;i<roles.length;i++){
            values+=" ('"+userId+"','"+roles[i]+"'),";
            roleStr+="'"+roles[i]+"',";
        }
        values=values.substring(0,values.length()-1);
        roleStr=roleStr.substring(0,roleStr.length()-1)+")";

        if(roles.length==0){
            user.setIdentity("无");
            baseMapper.user_role_del(userId);
            userMapper.updateUserRole(user);

        }else{
            baseMapper.user_role_del(userId);
            baseMapper.user_role_add(values);
            String str="";
            List<String> list=roleMapper.selectNameByIdStr(roleStr);
            for(String demo:list){
                str+=demo+",";
            }
            str=str.substring(0,str.length()-1);
            user.setIdentity(str);
            userMapper.updateUserRole(user);
        }
    }

    @Override
    public void systemLogAdd(SystemLog systemLog) {
        baseMapper.systemLogAdd(systemLog);
    }

    @Override
    public void edtPwd(Map map) {
        baseMapper.modifyPwd(map);
    }
}
